package com.homegoods.orderinfodetail.presenter;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.fastjson.JSON;
import com.homegoods.classify.model.Constant;
import com.homegoods.classify.model.VideoConstant;
import com.homegoods.goods.model.GoodsModel;
import com.homegoods.orderinfodetail.OrderInfoApiUtils;
import com.homegoods.orderinfodetail.adapter.ShopDetailAdapter;
import com.homegoods.orderinfodetail.bean.ShopDetailBean;
import com.homegoods.orderinfodetail.item.ShopDetailHeaderItem;
import com.homegoods.orderinfodetail.item.ShopDetailHotGoodsItem;
import com.homegoods.orderinfodetail.item.ShopDetailVideoIntroItem;
import com.homegoods.orderinfodetail.model.ShopDetailGoodsModel;
import com.homegoods.orderinfodetail.model.ShopDetailModel;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.AbstractRefreshPresenter;
import com.kedll.library.mvp.RefreshingView;
import com.kedll.library.util.MyUtils;

import java.util.List;
import java.util.Locale;
import java.util.Random;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 */
public class ShopDetailPresenter extends AbstractRefreshPresenter<RefreshingView> {
    private ShopDetailHotGoodsItem hotGoodsItem;
    private ShopDetailVideoIntroItem videoIntroItem;
    private ShopDetailHeaderItem headerItem;
    private String shopSid;

    public ShopDetailPresenter(RefreshingView view) {
        super(view);
        hotGoodsItem = new ShopDetailHotGoodsItem();
        videoIntroItem = new ShopDetailVideoIntroItem();
        headerItem = new ShopDetailHeaderItem();
    }


    @Override
    protected void addHead() {
        super.addHead();
        adapter.addItem(headerItem);
        adapter.addItem(videoIntroItem);
        adapter.addItem(hotGoodsItem);
//        loadHeadData();
        adapter.notifyItemRangeInserted(0, 3);

    }

    public void getShop(String sid) {
        shopSid = sid;
        OrderInfoApiUtils.getShopDetails(sid, subModelList -> {
            List<ShopDetailBean> shopBean = JSON.parseArray(subModelList.getItems(), ShopDetailBean.class);
            loadHeadData(shopBean.get(0));
        }, throwable -> {

        });
    }

    /**
     * 请求头部数据
     */
    private void loadHeadData(ShopDetailBean bean) {
        headerItem.setShopName(bean.getName());
        headerItem.setShopPicUrl(Constant.TEST_IMG_URL + 1);
        headerItem.setFans(bean.getAttenum());
        headerItem.setIntro(bean.getIntro());
        videoIntroItem.setVideoUrl(bean.getVediourl());
        videoIntroItem.setThumUrl(VideoConstant.videoThumbList[0]);
        hotGoodsItem.setHotGoodsPicUrl(Constant.TEST_IMG_URL + 2);
    }

    @Override
    protected void fetchData(final boolean loadMore) {
        OrderInfoApiUtils.getShopDetailsCommodityList(shopSid, subModelList -> {
            List<ShopDetailGoodsModel> goodsModels = JSON.parseArray(subModelList.getItems(), ShopDetailGoodsModel.class);
            parserData(loadMore, goodsModels);
        }, throwable -> {

        });
//        addSubscription(Observable.range(0, 11)
//                .map(integer -> {
//                    GoodsModel model = new GoodsModel();
//                    model.setId(String.valueOf(PER_PAGE_COUNT * page + integer));
//                    model.setBgimg(Constant.TEST_IMG_URL + new Random().nextInt(100));
//                    model.setTitle("美的（Midea）电饭煲 钻石纹理上盖 金属拉丝机身");
//                    model.setRegularprice(String.format(Locale.ROOT, "%.2f", new Random(10000).nextFloat()));
//                    model.setCurrentprice(String.format(Locale.ROOT, "%.2f", new Random(3000).nextFloat()));
//                    model.setOceannum(String.valueOf(new Random().nextInt(7000)));
//                    model.setLabel2(1);
//                    model.setShopName("CK制造商");
//                    model.setQuantity(String.valueOf(new Random().nextInt(500)));
//                    return model;
//                })
//                .toList()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(rs -> parserData(loadMore, rs))
//        );
    }

    @Override
    public MultiTypeAdapter getAdapter() {
        adapter = new ShopDetailAdapter(mView.getContext(), 2, MyUtils.dp2px(8));
        return adapter;
    }
}